class SolutionZsh {
    public String longestPalindrome(String s) {

        if (s == null || s.length() <= 1) return s;
        int left = 0;
        int right = 0;
        int currentMaxLength = 0;
        for (int i = 0; i < s.length(); i++) {
            int value = Math.max(calculate(s, i, i), calculate(s, i, i + 1));
            if (value > currentMaxLength) {
                left = i - (value - 1) / 2;
                right = i + value / 2;
                currentMaxLength = value;
            }
        }

        return s.substring(left, right + 1);
    }

    public int calculate(String s, int left, int right) {

        while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) {
            left--;
            right++;
        }
        return right - left - 1;
    }
}